﻿using System; 
using System.Collections.Generic; 
using System.ComponentModel; 

namespace DataAccess{
[Serializable]
[GenWiseAttribute(IsDataSourceEntity = true) ]
public class forum : INotifyPropertyChanged
 {


#region --- Class Fields / Objects  ---

[NonSerialized]
protected PropertyChangedEventHandler _propertyChanged;
private System.Int32 _id;
private System.String _name;
private System.String _description;
private System.Int32? _owner_id;
private IList<comment> _comments;
private IList<forum_to_User> _forum_to_Users;
private user _user;

#endregion


#region --- Class Properties ---

public virtual System.Int32 id 
{
get
{
return _id;
}
}

public virtual System.String name 
{
get
{
return _name;
}
set 
{
if ( _name != value) 
{
_name = value ;
OnPropertyChanged("name");
}
}
}

public virtual System.String description 
{
get
{
return _description;
}
set 
{
if ( _description != value) 
{
_description = value ;
OnPropertyChanged("description");
}
}
}

public virtual System.Int32? owner_id 
{
get
{
if ( user != null ) 
{
_owner_id = (System.Int32)user.id;
}

return _owner_id;
}
set 
{
if ( _owner_id != value) 
{
// Simplified Parent Access Property : reset object reference if simple property changes it's value
_user = null;

_owner_id = value ;
OnPropertyChanged("owner_id");
}
}
}

/// <summary>
/// Collection of comment.
/// </summary>
/// <remarks>
/// This has been automatically being generated because of the mapped 1:Many Relation 
/// </remarks>
[GenWiseAttribute(RelationID = "11acce2c-5e38-4fc2-a7b4-38d8642a9390" , CollectionOfType = typeof(comment)) ]
public virtual IList<comment> comments 
{
get
{
 if (_comments == null) _comments = new List<comment>();

return _comments;
}
set 
{
if ( _comments != value) 
{
_comments = value ;
}
}
}

/// <summary>
/// Collection of forum_to_User.
/// </summary>
/// <remarks>
/// This has been automatically being generated because of the mapped 1:Many Relation 
/// </remarks>
[GenWiseAttribute(RelationID = "4515b1e8-732b-4873-b086-93c6aef372b6" , CollectionOfType = typeof(forum_to_User)) ]
public virtual IList<forum_to_User> forum_to_Users 
{
get
{
 if (_forum_to_Users == null) _forum_to_Users = new List<forum_to_User>();

return _forum_to_Users;
}
set 
{
if ( _forum_to_Users != value) 
{
_forum_to_Users = value ;
}
}
}

/// <summary>
/// Generated from Relation FK_Forum_User
/// </summary>
[GenWiseAttribute(RelationID = "1f5341be-96c9-4c0c-9e03-db96e6267eab") ]
public virtual user user 
{
get
{
if ( _user == null ) 
{
if ( !_owner_id.HasValue || _owner_id.Value.Equals(System.Int32.MinValue) ) 
{
if ( _user != null && ( _user.id.Equals(null) ) )
{
  _user = null;
}
}
else
{
_user = (new userFactory()).Get(_owner_id.Value);
}
}

return _user;
}
set 
{
if ( _user != value) 
{
if ( value==null ) _owner_id = null; else _owner_id = (System.Int32)value.id; 
_user = value ;
}
}
}

/// <summary>
/// Is this object Valid? 
/// </summary>
/// <remarks>
/// This is achieved by counting how many errors are returned from the Validate() method. If no errors are found then it's valid.
/// </remarks>
[Bindable(false)]
public virtual System.Boolean IsValid 
{
get
{
List<Error> _errors = this.Validate(); if ( _errors.Count > 0 ) return false; else return true;
}
}


#endregion


#region --- Class Methods --- 

/// <summary>
/// Raises PropertyChanged event.
/// </summary>
protected virtual void OnPropertyChanged(System.String ppropertyChanged)
{
if (_propertyChanged != null) _propertyChanged( this , new PropertyChangedEventArgs(ppropertyChanged));

}

/// <summary>
/// Create a new forum object. 
/// </summary>
public  forum()
{
_id = System.Int32.MinValue;

}

/// <summary>
/// Add a new comment object and automatically associate it with this instance as it's parent. This method makes easier to add new children and ensures you do not forget to related them correctly.
/// </summary>
///<param name="pcomment">An comment object to be added to this objects collection.</param>
public virtual void Addcomment(comment pcomment)
{
if( pcomment == null  ) throw new ArgumentNullException("pcomment");
// Set the Parent Property
pcomment.forum = this;
if (!pcomment.IsValid) { throw new Exception("pcomment failed on the Validate() method." + Error.ErrorsToString( pcomment.Validate() ) ); }
comments.Add(pcomment);
}

/// <summary>
/// Delete an existing comment object + remove the association it with this instance as it's parent. This method makes easier to add new children and ensures you do not forget to related them correctly.
/// </summary>
///<param name="pcomment">The comment object to be removed from this objects collection.</param>
public virtual void Deletecomments(comment pcomment)
{
if( pcomment == null  ) throw new ArgumentNullException("pcomment");
comments.Remove(pcomment);
// Remove the Association with the Parent object
pcomment.forum = null;
}

/// <summary>
/// Add a new forum_to_User object and automatically associate it with this instance as it's parent. This method makes easier to add new children and ensures you do not forget to related them correctly.
/// </summary>
///<param name="pforum_to_User">An forum_to_User object to be added to this objects collection.</param>
public virtual void Addforum_to_User(forum_to_User pforum_to_User)
{
if( pforum_to_User == null  ) throw new ArgumentNullException("pforum_to_User");
// Set the Parent Property
pforum_to_User.forum = this;
if (!pforum_to_User.IsValid) { throw new Exception("pforum_to_User failed on the Validate() method." + Error.ErrorsToString( pforum_to_User.Validate() ) ); }
forum_to_Users.Add(pforum_to_User);
}

/// <summary>
/// Delete an existing forum_to_User object + remove the association it with this instance as it's parent. This method makes easier to add new children and ensures you do not forget to related them correctly.
/// </summary>
///<param name="pforum_to_User">The forum_to_User object to be removed from this objects collection.</param>
public virtual void Deleteforum_to_Users(forum_to_User pforum_to_User)
{
if( pforum_to_User == null  ) throw new ArgumentNullException("pforum_to_User");
forum_to_Users.Remove(pforum_to_User);
// Remove the Association with the Parent object
pforum_to_User.forum = null;
}

/// <summary>
/// Validates the object returning a list of errors if the validation fails.
/// </summary>
/// <remarks>
/// You can use the provided User CodeBlocks to add your own validations.
/// </remarks>
///<returns>Returns the List of Errors found during the Validation. If no errors are found null is returned.<returns>
public virtual List<Error> Validate()
{
List<Error> _errors = new List<Error>();
return _errors;
}


#endregion


#region --- Class Events  ---

public event PropertyChangedEventHandler PropertyChanged 
{
   add {
_propertyChanged+= value;
   }
   remove {
_propertyChanged-= value;
   }
}


#endregion
}
}

